package com.shigu.agent.model.impl;

import com.opentae.data.mall.beans.ManagerUser;
import com.opentae.data.mall.beans.ManagerUserRole;
import com.opentae.data.mall.interfaces.ManagerUserMapper;
import com.opentae.data.mall.interfaces.ManagerUserRoleMapper;
import com.shigu.agent.bo.CardBO;
import com.shigu.agent.bo.UserBO;
import com.shigu.agent.enums.UserStatus;
import com.shigu.agent.exceptions.YeepayException;
import com.shigu.agent.model.MoUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.PostConstruct;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Date;
import java.util.List;

/**
 * 普通用户
 */
@Component
@Scope("prototype")
public class MoUserImpl implements MoUser{
    private Long userId;

    private UserBO userinfo;

    public static final Long defaultAgentId=1L;//默认的代理ID

    @Autowired
    private ManagerUserMapper managerUserMapper;

    @Autowired
    private ManagerUserRoleMapper managerUserRoleMapper;

    public MoUserImpl() {
    }

    public MoUserImpl(UserBO userinfo){
        this.userinfo=userinfo;
    }

    public MoUserImpl(Long userId) {
        this.userId = userId;
    }

    @PostConstruct
    public void init() throws YeepayException {
        if (userId == null&&userinfo!=null) {
            this.userId=createOne(userinfo);
        }
    }
    public boolean changePwd(String pwd) {
        ManagerUser user=new ManagerUser();
        user.setUserId(userId);
        user.setPassword(pwd);
        return managerUserMapper.updateByPrimaryKeySelective(user)>0;
    }

    @Transactional(rollbackFor = Exception.class)
    public Long createOne(UserBO userinfo) {
        //先添加用户
        ManagerUser user=new ManagerUser();
        user.setPassword(userinfo.getPassword());
        user.setNick(userinfo.getNick());
        Date now=new Date();
        user.setCreateTime(now);
        user.setLoginName(userinfo.getUserName());
        user.setStatus(UserStatus.NORMAL.getValue());
        user.setHeadUrl(userinfo.getHeadUrl());
        user.setOemTag(userinfo.getOemTag());
        user.setAgentId(defaultAgentId);
        managerUserMapper.insertSelective(user);
        this.userId=user.getUserId();
        List<Long> roles=userinfo.getRoleIds();
        //再添加权限
        if (roles != null&&roles.size()>0) {
            List<ManagerUserRole> userRoles=new ArrayList<>();
            for(Long rid:roles){
                ManagerUserRole role=new ManagerUserRole();
                role.setCreateTime(now);
                role.setRoleId(rid);
                role.setUserId(user.getUserId());
                userRoles.add(role);
            }
            managerUserRoleMapper.insertListNoId(userRoles);
        }
        return user.getUserId();
    }

    public void abledMe(boolean able) {
        ManagerUser user=new ManagerUser();
        user.setUserId(userId);
        user.setStatus(able?UserStatus.NORMAL.getValue():UserStatus.STOP.getValue());
        managerUserMapper.updateByPrimaryKeySelective(user);
    }

    public Long bindCard(CardBO card) {

        return null;
    }

    public Long getUserId() {
        return userId;
    }

    public void setUserId(Long userId) {
        this.userId = userId;
    }
}
